package com.resource.transactions.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.resource.transactions.entities.RUser;
import com.resource.transactions.entities.RUserRole;
import com.resource.transactions.entities.Result;
import com.resource.transactions.service.RUserRoleService;
import com.resource.transactions.service.RUserService;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@SessionAttributes("userLoginInFo")
public class UserController {

    @Resource
    private RUserService rUserService;
    @Resource
    private RUserRoleService rUserRoleService;


    @RequestMapping("userLoginError")
    @ResponseBody
    public Result userLoginError() {
        return Result.FAIL();
    }

    @RequestMapping("userLoginSuccess")
    @ResponseBody
    public Result userLoginSuccess(@AuthenticationPrincipal UserDetails userDetails, ModelMap model) {
        RUser rUser = rUserService.userInFoSelectOne(userDetails.getUsername());
        model.addAttribute("userLoginInFo",rUser);
        return Result.SUCCESS();
    }

    @RequestMapping("/smsverification")
    @ResponseBody
    public Object SmsVerification(@Param("phone") String phone) {
        return rUserService.SmsVerification(phone);
    }

    @RequestMapping("userInFoInsert")
    @ResponseBody
    public Result userInFoInsert(RUser rUser) {
        rUser.setUserdatetime(new Date());
        rUser.setUserprofile("assets/img/users/default.jpg");
        rUser.setUserpasw(new BCryptPasswordEncoder().encode(rUser.getUserpasw()));
        Integer integer = rUserService.userInFoInsert(rUser);
        if (integer > 0) {
            RUserRole rUserRole = new RUserRole();
            rUserRole.setRoleid(1);
            rUserRole.setUserid(rUser.getUserid());
            rUserRoleService.rUserRoleInFoInsert(rUserRole);
            return Result.SUCCESS();
        } else {
            return Result.FAIL();
        }
    }

    @RequestMapping("userInFoUpdate")
    @ResponseBody
    public Result userInFoUpdate(String userpasw, String useremail) {
        RUser rUser = new RUser();
        rUser.setUserpasw(new BCryptPasswordEncoder().encode(userpasw));
        UpdateWrapper<RUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("useremail", useremail);
        Integer integer = rUserService.userInFoUpdate(rUser, updateWrapper);
        if (integer > 0) {
            return Result.SUCCESS();
        } else {
            return Result.FAIL();
        }
    }
}
